package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AppUserDTO;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.mapper.ApUserRealnameMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author tmh
 * @description 针对表【ap_user_realname(APP实名认证信息表)】的数据库操作Service实现
 * @createDate 2023-08-28 09:53:17
 */
@Service
@Slf4j
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
        implements ApUserRealnameService {
    public static final Integer AUTH_FAIL = 2;
    public static final Integer AUTH_PASS = 9;

    @Resource
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Resource
    private ApUserMapper apUserMapper;

    /**
     * 用户审核查询列表
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult userAuthList(AuthDto authDto) {
        //1.判断参数是否有效
        if (authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.创建分页Page对象
        Integer currentPage = authDto.getPage();
        Integer size = authDto.getSize();
        IPage<ApUserRealname> page = new Page(currentPage, size);
        page = page(page, null);
        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 用户审核失败
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {

        //1.校验参数
        if (authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.根据id查询对应的实体
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(authDto.getId());
        //3.修改状态，以及添加驳回原因
        apUserRealname.setStatus(AUTH_FAIL);
        apUserRealname.setReason(authDto.getMsg());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户审核成功
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        //1.判断参数是否有效
        if (authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改审核状态为通过，也就是9
        //2.1查询对应实体
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(authDto.getId());
        //2.2修改审核状态通过
        apUserRealname.setStatus(AUTH_PASS);
        updateById(apUserRealname);
        //3.查询用户id
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);

        //创建appUserDTO
        AppUserDTO appUserDTO = new AppUserDTO();
        BeanUtils.copyProperties(apUser,appUserDTO);
        appUserDTO.setApUserId(apUser.getId());
        //5.创建自媒体账号(这个账号的用户名与密码跟app一致)---涉及到两个微服务，这里采用openFeign远程接口调用
        iWemediaClient.addWemediaUser(appUserDTO);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




